package com.campusflea;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.campusflea.fragment.FragmentMain;
import com.campusflea.fragment.FragmentMy;
import com.campusflea.fragment.FragmentRelease;
import com.campusflea.person.Person_Godown;
import com.campusflea.person.Person_about;
import com.campusflea.person.Person_donate;
import com.campusflea.person.Person_gain;
import com.campusflea.person.Person_sale;


public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {
        private BottomNavigationBar mBottomBar;
        private FragmentMain fragmentMain;
        private FragmentRelease fragmentRelease;
        private FragmentMy fragmentMy;
        private String phone;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Intent intent = getIntent();
            phone = intent.getExtras().getString("phone");
            assignViews();
        }
    /**
     * 聊天界面的点击事件
     */
        public void click(View v)
        {
            switch (v.getId()){
                case R.id.button:
                    Intent intent=new Intent(MainActivity.this,ChatActivity.class);
                    startActivity(intent);
                    break;
            }

        }
        /**
         * 添加页面
         */
        private void assignViews() {
            mBottomBar = (BottomNavigationBar) findViewById(R.id.bottom_bar);
            mBottomBar.setActiveColor(R.color.black).setInActiveColor(R.color.white).setBarBackgroundColor(R.color.lightskyblue);
            mBottomBar.setMode(BottomNavigationBar.MODE_FIXED);
            mBottomBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC );
            mBottomBar.addItem(new BottomNavigationItem(R.mipmap.icon_main1,"主页").setInactiveIcon(ContextCompat.getDrawable(this, R.mipmap.icon_main)));
            mBottomBar.addItem(new BottomNavigationItem(R.mipmap.icon_release1,"发布").setInactiveIcon(ContextCompat.getDrawable(this, R.mipmap.icon_release)));
            mBottomBar.addItem(new BottomNavigationItem(R.mipmap.icon_my1,"我的").setInactiveIcon(ContextCompat.getDrawable(this, R.mipmap.icon_my)));
            mBottomBar.setFirstSelectedPosition(0);
            mBottomBar.initialise();
            mBottomBar.setTabSelectedListener(this);//设置监听


            setDefaultFragment();//设置默认选项
        }
        private void setDefaultFragment() {
            FragmentManager fragmentManager=getSupportFragmentManager();
            FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
            hideFragments(fragmentTransaction);
            fragmentMain=new FragmentMain(phone);
            fragmentTransaction.add(R.id.ll_root,fragmentMain);
            fragmentTransaction.commit();
        }

        /**
         * 隐藏fragment
         * @param transaction
         */
        private void hideFragments(FragmentTransaction transaction) {
            if (fragmentMain != null) {
                transaction.hide(fragmentMain);
            }
            if (fragmentRelease != null) {
                transaction.hide(fragmentRelease);
            }
            if (fragmentMy != null) {
                transaction.hide(fragmentMy);
            }
        }

        @Override
        public void onTabSelected(int position) {
            FragmentManager fragmentManager=getSupportFragmentManager();
            FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
            hideFragments(fragmentTransaction);
            switch (position){
                case 0:
                    if (fragmentMain==null){
                        fragmentMain=new FragmentMain(phone);
                        fragmentTransaction.add(R.id.ll_root,fragmentMain);

                    }else {
                        fragmentTransaction.show(fragmentMain);
                    }
                    break;
                case 1:
                    if (fragmentRelease==null){
                        fragmentRelease=new FragmentRelease(phone);
                        fragmentTransaction.add(R.id.ll_root,fragmentRelease);

                    }else {
                        fragmentTransaction.show(fragmentRelease);
                    }
                    break;
                case 2:
                    if (fragmentMy==null){
                        fragmentMy=new FragmentMy(phone);
                        fragmentTransaction.add(R.id.ll_root,fragmentMy);

                    }else {
                        fragmentTransaction.show(fragmentMy);
                    }
                    break;
            }
            fragmentTransaction.commit();
        }

        @Override
        public void onTabUnselected(int position) {

        }

        @Override
        public void onTabReselected(int position) {

        }
}
